import SendMsg from "@/api/SendMsg";
import {message} from "ant-design-vue";

const ErrorCode = {
    namespaced: true,
    state: {
        errorCodeForm:{
            transactionNo: '',
            errorCode:'',
            systemMessage:'',
            message:'',
            action:'',
            actionDetail:''
        },
        errorCodeVisible: false,
        updateButtonLoading: false,
        ignoreErrorCode: []
    },
    mutations: {
        setErrorCodeVisible(state, val){
            state.errorCodeVisible = val
        },
        setIgnoreErrorCode(state, val){
            state.ignoreErrorCode = val
        },
        setErrorCodeForm(state, val){
            state.errorCodeForm = {
                transactionNo: val.trxId,
                errorCode: val.rtnCode,
                systemMessage: val.rtnMesg,
                message: val.exception,
                action:'',
                actionDetail:''
            }
        },
        clearErrorForm(state, val){
            state.errorCodeForm = {
                transactionNo: '',
                errorCode:'',
                systemMessage:'',
                message:'',
                action:'',
                actionDetail:''
            }
        },
        setErrorCodeFormAction(state, data){
            state.errorCodeForm = {
                ...state.errorCodeForm,
                action: data.actBody,
                actionDetail: data.actDtl
            }
        }
    },
    actions: {
        updateFunc({state, commit, dispatch}, errorCodeForm){
            let inTrx = {
                trxId: 'retMsg',
                actionFlg: 'U',
                msgId: errorCodeForm.errorCode,
                actBody: errorCodeForm.action,
                actDtl: errorCodeForm.actionDetail,
            }
            state.updateButtonLoading = true
            SendMsg.sendPostTrx(inTrx).then(({data}) => {
                if(data.rtnCode !== '0000000'){
                    return
                }
                dispatch('queryErrorAction', errorCodeForm.errorCode)
                message.success('操作信息更新成功')
            }).finally(()=>{
                state.updateButtonLoading = false
            })
        },
        queryErrorAction({state, commit}, errCode){
            let inTrx = {
                trxId: 'retMsg',
                actionFlg: 'Q',
                msgId: errCode
            }
            SendMsg.sendPostTrx(inTrx).then(({data}) => {
                if(data.rtnCode !== '0000000'){
                    return
                }
                commit('setErrorCodeFormAction', data)
            })
        }
    }
};

export default ErrorCode;
